﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Configuration;
using System.Web;
using RelightERP.DAL.EntityModel;
using RelightERP.BO.Entities;

namespace RelightERP.BLL
{
    public class DefaultHelper
    {
        #region Variables
        internal static XElement xSql = XElement.Load(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["Sql"]));
        //internal static XElement xReportDetails = XElement.Load("~/Xml/XReportSql.xml");
        #endregion

        #region GetSql
        public static string GetSql(string sqlID)
        {
            return (xSql.Elements()
                    .FirstOrDefault(x => x.Attribute("ID").Value == sqlID)
                    .Value);
        }
        #endregion

        #region GetConnString
        public static string GetConnString()
        {
            return (ConfigurationManager.ConnectionStrings["ConnectDB"].ConnectionString);
        }
        #endregion

        #region GetReportSql
        //public static string GetReportSql(string reportID, string sqlID)
        //{
        //    //Get Report XML
        //    XElement xElement = xReportDetails.Elements("Report").SingleOrDefault(x => x.Attribute("ID").Value == reportID);
        //    return (xElement.Elements("Sql").SingleOrDefault(x => x.Attribute("ID").Value == sqlID).Value);
        //}
        #endregion

        /// <summary>
        /// Append PREFIX first.
        ///	If Running no. < 10 then [Append two ZERO(0) and then the Running no.]
        ///	If Running no. >= 10 And < 100 then [Append one ZERO(0) and then the Running no.]
        ///	If Running no. >= 100 then [Append only Running no.]
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="uniqueNumber"></param>
        /// <returns></returns>
        public static string GenerateRelightUniqueCode(string prefix, long runningNumber, string suffix)
        {
            if (runningNumber <= 0) throw new ArgumentNullException("runningNumber");

            string uniqueCode = string.Empty;
            string runningNumberText = string.Empty;
            if (!string.IsNullOrEmpty(prefix))
                uniqueCode = prefix;

            if (runningNumber < 10)
                runningNumberText = "00" + runningNumber.ToString();
            else if (runningNumber >= 10 && runningNumber < 100)
                runningNumberText = "0" + runningNumber.ToString();
            else if (runningNumber >= 100)
                runningNumberText = runningNumber.ToString();

            if (!string.IsNullOrEmpty(runningNumberText))
                uniqueCode += runningNumberText;

            if (!string.IsNullOrEmpty(suffix))
                uniqueCode += suffix;

            return uniqueCode;
        }

        public static UniqueCodeSetup GetUniqueCodeSetup(RelightDBEntities context, UniqueCodeType uniqueCodeType)
        {
            UniqueCodeSetup codeSetup = null;
            int codeID = uniqueCodeType.GetHashCode();            
            Setup branchSetup = context.Setup.FirstOrDefault(x => x.ID == codeID);

            if (branchSetup != null)
            {
                codeSetup = new UniqueCodeSetup();
                XElement xRoot = XElement.Parse(branchSetup.XmlValue);
                if (xRoot.Element("Prefix") != null)
                    codeSetup.Prefix = xRoot.Element("Prefix").Value;

                if (xRoot.Element("Suffix") != null)
                    codeSetup.Suffix = xRoot.Element("Suffix").Value;

                codeSetup.LastUniqueNumber = branchSetup.IntValue.Value;
            }
            return codeSetup;
        }
    }
}
